
from logging import exception
from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class BoostConan(ConanFile):
    name = "boost"
    version = "1.6.7"
    url = "https://boostorg.jfrog.io/artifactory/main/release/1.67.0/source/boost_1_67_0.7z"
    description = "boost"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"

    def source(self):
        my_file = Path("pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            tools.get(self.url, destination=".")
            tools.unzip('boost_1_67_0.7z', "pkgsrc")

    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe", dst="", keep_path=True)

    def build(self):
        print("path " + self.source_folder + " is where the package built")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print(
                        "msys boost build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                          bashcmd="./bootstrap.sh  gcc ")
                tools.run_in_windows_bash(
                    self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="./b2 -j 16 toolset=gcc stage")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                          bashcmd="./b2 -j 16 install --prefix=" + tools.unix_path(self.package_folder))
                # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
                os.remove(tools.get_env("CONAN_SH_PATH"))
            if tools.get_env("BUILD_ENV") == "msvc64":
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    print("path is \r\n", tools.get_env("PATH"))
                    self.run("bootstrap.bat", True, cwd="pkgsrc")
                    self.run("b2 --prefix=\"" + self.package_folder +
                             "\" install", True, cwd="pkgsrc")

    def package_info(self):
        self.cpp_info.name = 'boost'
        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "boost"
                self.cpp_info.names["cmake_find_package_multi"] = "boost"
                # Directories where libraries can be found
                self.cpp_info.libdirs = ['lib']
                self.cpp_info.buildirs = ''
                self.cpp_info.includedirs = ['include\boost-1_71']
                self.cpp_info.name = "boost"
            if tools.get_env("BUILD_ENV") == "msvc64":
                print("msvc64 build")
                self.cpp_info.names["cmake_find_package"] = "boost"
                self.cpp_info.names["cmake_find_package_multi"] = "boost"
                # Directories where libraries can be found
                self.cpp_info.libdirs = ['lib']
                self.cpp_info.buildirs = ''
                self.cpp_info.includedirs = ['include\boost-1_71']
                self.cpp_info.name = "boost"
